package com.boot.main.exception;

import com.boot.main.exception.ExceptionEnum;
import com.boot.main.response.ResponseData;
import com.boot.main.response.ResponseDataUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authz.AuthorizationException;
import org.apache.shiro.authz.UnauthenticatedException;
import org.apache.shiro.authz.UnauthorizedException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.security.NoSuchAlgorithmException;

@Slf4j
@RestControllerAdvice  //不指定包默认加了@Controller和@RestController都能控制
public class ExceptionHandlerConfig {
    // 自定义异常
    @ExceptionHandler(value = CustomException.class)
    @ResponseBody
    public ResponseData exceptionHandler(CustomException e){
        return ResponseDataUtil.response(e.getCode(),e.getMsg(), null);
    }
    // 未知异常
    @ExceptionHandler(value = Exception.class)
    @ResponseBody
    public ResponseData exceptionHandler(Exception e){
        return ResponseDataUtil.response(ExceptionEnum.UNKNOWN.getCode(), ExceptionEnum.UNKNOWN.getMsg(), null);
    }
    //空指针异常
    @ExceptionHandler(value = NullPointerException.class)
    @ResponseBody
    public ResponseData exceptionHandler(NullPointerException e){
        return ResponseDataUtil.response(ExceptionEnum.IS_NOT_NULL.getCode(),ExceptionEnum.IS_NOT_NULL.getMsg(), null);
    }
    //没有访问权限
    @ExceptionHandler(value = UnauthorizedException.class)
    @ResponseBody
    public ResponseData unauthorizedException(UnauthorizedException ex) {
        return ResponseDataUtil.response(ExceptionEnum.FORBIDDEN.getCode(),ExceptionEnum.FORBIDDEN.getMsg(), null);
    }
    //没有登录
    @ExceptionHandler(value = UnauthenticatedException.class)
    @ResponseBody
    public ResponseData unauthenticatedException(UnauthenticatedException ex) {
        return ResponseDataUtil.response(ExceptionEnum.UNLOGIN.getCode(),ExceptionEnum.UNLOGIN.getMsg(), null);
    }
    //登录凭证过期
    @ExceptionHandler(value = AuthorizationException.class)
    @ResponseBody
    public ResponseData authorizationException(AuthorizationException ex) {
        return ResponseDataUtil.response(ExceptionEnum.UNAUTHORIZEN.getCode(),ExceptionEnum.UNAUTHORIZEN.getMsg(), null);
    }
    //认证异常
    @ExceptionHandler(value = AuthenticationException.class)
    @ResponseBody
    public ResponseData authenticationException(AuthenticationException ex) {
        return ResponseDataUtil.response(ExceptionEnum.TOKENNULL.getCode(),ExceptionEnum.TOKENNULL.getMsg(), null);
    }
    //文件上传异常
    @ExceptionHandler(value = IOException.class)
    @ResponseBody
    public ResponseData ioException(IOException ex) {
        return ResponseDataUtil.response(ExceptionEnum.FILE_UPLOAD_ERROR.getCode(),ExceptionEnum.FILE_UPLOAD_ERROR.getMsg(), null);
    }
    // 参数异常
    @ExceptionHandler(value = IllegalArgumentException.class)
    @ResponseBody
    public ResponseData illegalArgumentException(IllegalArgumentException e) {
        return ResponseDataUtil.response(ExceptionEnum.PARAM_ERROR.getCode(),ExceptionEnum.PARAM_ERROR.getMsg(), null);
    }
    // 参数异常
    @ExceptionHandler(value = NoSuchAlgorithmException.class)
    @ResponseBody
    public ResponseData noSuchAlgorithmException(NoSuchAlgorithmException e) {
        return ResponseDataUtil.response(ExceptionEnum.PARAM_ERROR.getCode(),ExceptionEnum.PARAM_ERROR.getMsg(), null);
    }
    // 参数异常
    @ExceptionHandler(value = UnsupportedEncodingException.class)
    @ResponseBody
    public ResponseData unsupportedEncodingException(UnsupportedEncodingException e) {
        return ResponseDataUtil.response(ExceptionEnum.PARAM_ERROR.getCode(),ExceptionEnum.PARAM_ERROR.getMsg(), null);
    }
}
